package lzzy1;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 @WebServlet(name = "PurchaseServlet",urlPatterns = "/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id =req.getParameter("id");
        if(id==null){
            String url = "ListCakeServlet";
            resp.sendRedirect(url);
            return;
        }
        Cake cake = CakeDB.getCake(id);

        HttpSession session = req.getSession();

        List<Cake>cart = (List<Cake>) session.getAttribute("cart");
        if(cart==null){
            cart=new ArrayList<Cake>();

            session.setAttribute("cart",cart);
        }
        cart.add(cake);
        Cookie cookie = new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(60*30);
        cookie.setPath("/Serlet");
        resp.addCookie(cookie);

        String url="CartServlet";
        resp.sendRedirect(url);

    }
}
